package tongwentongshu.com.app.activity;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import tongwentongshu.com.app.R;
import tongwentongshu.com.app.adapter.MyShareAdapter;
import tongwentongshu.com.app.adapter.SearchAdapter;
import tongwentongshu.com.app.bean.BookSearchBean;
import tongwentongshu.com.app.bean.MyCollectionBean;
import tongwentongshu.com.app.bean.MyShareBean;
import tongwentongshu.com.app.contract.MyShareContract;
import tongwentongshu.com.app.contract.SearchResultContract;
import tongwentongshu.com.app.presenter.MySharePresenter;
import tongwentongshu.com.app.presenter.SearchResultPresenter;

/**
 * Created by XiongBin on 2017/7/18.
 */

public class SearchResultActivity extends BaseActivity implements SearchResultContract.View, BaseQuickAdapter.RequestLoadMoreListener, SwipeRefreshLayout.OnRefreshListener {

    @BindView(R.id.rv_list)
    RecyclerView mRecyclerView;
    @BindView(R.id.swipeLayout)
    SwipeRefreshLayout mSwipeRefreshLayout;
    private SearchAdapter searchAdapter;
    protected boolean onRefreshJudge = false;
    private SearchResultPresenter searchResultPresenter;

    @Override
    protected int getLayoutResourceId() {
        return R.layout.activity_my_share;
    }

    @Override
    protected int getTitleId() {
        return R.string.search_result;
    }

    @Override
    protected void initView() {
        onRefreshJudge = true;
        mSwipeRefreshLayout.setOnRefreshListener(this);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        List arr = new ArrayList<MyCollectionBean.DataBean>();
        searchAdapter = new SearchAdapter(mContext, arr);
        searchAdapter.setOnLoadMoreListener(this, mRecyclerView);
        searchAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);

        mRecyclerView.setAdapter(searchAdapter);
        mRecyclerView.addOnItemTouchListener(new OnItemClickListener() {
            @Override
            public void onSimpleItemClick(final BaseQuickAdapter adapter, final View view, final int position) {
                Bundle b = new Bundle();
                b.putBoolean("pager_Judge", true);
                b.putString("pagerType", "1");
                b.putString("bookId", msg.getData().get(position).getId());
                startActivity(ReadingDetailsActivity.class, b);
            }
        });
    }

    @Override
    protected void initData() {
        searchResultPresenter = new SearchResultPresenter(this);
        searchResultPresenter.getSearch(mContext);
    }

    BookSearchBean msg;

    @Override
    public void onSuccess(BookSearchBean msg) {
        View empty = getLayoutInflater().inflate(R.layout.empty_no_data, (ViewGroup) mRecyclerView.getParent(), false);
        //pullToRefreshAdapter.setPreLoadNumber(3);
        searchAdapter.setEmptyView(empty);
        this.msg = msg;
        if (msg.getData() == null || msg.getData().size() > 0) {
            if (onRefreshJudge) {
//                Log.e(TAG, "onRefreshJudge: " + onRefreshJudge);
                searchAdapter.setNewData(msg.getData());
                mSwipeRefreshLayout.setRefreshing(false);
                searchAdapter.setEnableLoadMore(true);
                onRefreshJudge = false;
            }
        } else {
            mSwipeRefreshLayout.setRefreshing(false);
            searchAdapter.setEnableLoadMore(true);
            onRefreshJudge = false;
        }
    }

    @Override
    public String getBookname() {
        return getIntent().getExtras().getString("bookname");
    }


    @Override
    public void onShowError(String msg) {

    }

    @Override
    public void onRefresh() {
        onRefreshJudge = true;
        searchResultPresenter.getSearch(mContext);
    }

    @Override
    public void onLoadMoreRequested() {
        searchAdapter.loadMoreEnd(true);
    }

}
